package com.learn.mail.config;

import org.beetl.core.GroupTemplate;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.io.IOException;
import java.util.Properties;


/**
 *JavaMailSender读取配置并注入到Spring的IOC容器中：
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2023/12/16 19:08
 * @updateUser: cong zhi
 * @updateTime: 2023/12/16 19:08
 * @updateRemark: 修改内容
 * @version: v1.0
 */
@Configuration
public class MailingConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public MailProperties mailProperties() {
        return new MailProperties();
    }


    @Bean
    public JavaMailSender javaMailSender(MailProperties mailProperties) {
        final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(mailProperties.getHost());
        mailSender.setUsername(mailProperties.getUsername());
        mailSender.setPassword(mailProperties.getPassword());

        final Properties props = mailSender.getJavaMailProperties();

        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", true);
        props.put("mail.smtp.starttls.enable",true);
        props.put("mail.debug", "true");
        return mailSender;
    }
    @Bean
    public GroupTemplate groupTemplate() throws IOException {
        ClasspathResourceLoader loader = new ClasspathResourceLoader("templates/");
        org.beetl.core.Configuration cfg = org.beetl.core.Configuration.defaultConfiguration();
        return new GroupTemplate(loader, cfg);
    }

}
